package com.kchouse.mine;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.kchouse.MainActivity;
import com.kchouse.R;
import com.kchouse.common.API;
import com.kchouse.common.BaseActivity;
import com.kchouse.common.MyHttpCycleContext;
import com.kchouse.common.ToastUtils;
import com.lljjcoder.Interface.OnCityItemClickListener;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;
import com.lljjcoder.citywheel.CityConfig;
import com.lljjcoder.style.citypickerview.CityPickerView;

import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;

public class NewAddressActivity extends BaseActivity implements View.OnClickListener {

    private NewAddressActivity mContent;
    private TextView tv_title;
    private ImageView iv_back;
    private View rl_area;
    private CityPickerView mPicker;
    private TextView tv_area;
    private TextView tv_subsave;
    private EditText et_name;
    private EditText et_phone;
    private EditText et_address;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_address);

        mContent = this;
        intiData();
        intiView();
    }

    private void intiView() {
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("新增地址");
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(this);

        rl_area = (View) findViewById(R.id.rl_area);
        rl_area.setOnClickListener(this);
        tv_area = (TextView) findViewById(R.id.tv_area);
        tv_subsave = (TextView) findViewById(R.id.tv_subsave);
        tv_subsave.setOnClickListener(this);

        et_name = (EditText) findViewById(R.id.et_name);
        et_phone = (EditText) findViewById(R.id.et_phone);
        et_address = (EditText) findViewById(R.id.et_address);
    }

    private void intiData() {
/**
 * 预先加载仿iOS滚轮实现的全部数据
 */
//申明对象
        mPicker = new CityPickerView();
        mPicker.init(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_area:
                inticity();
                break;
            case R.id.tv_subsave:
//                ToastUtils.getInstanc(mContent).showToast("提交");

                if(TextUtils.isEmpty(et_name.getText().toString())){
                    ToastUtils.getInstanc(mContent).showToast("您还未填写姓名");
                    return;
                }
                if(TextUtils.isEmpty(et_phone.getText().toString())){
                    ToastUtils.getInstanc(mContent).showToast("您还未填写电话");
                    return;
                }
                if(TextUtils.isEmpty(et_address.getText().toString())){
                    ToastUtils.getInstanc(mContent).showToast("您还未填写地址");
                    return;
                }

                PostNewAddress(et_name.getText().toString(),et_phone.getText().toString(),et_address.getText().toString());
                break;
        }
    }

    /**
     * 新增地址
     */
    private void PostNewAddress(String name,String phone,String address) {
        String url = API.PostNewAddress;
        RequestParams params = new RequestParams(new MyHttpCycleContext());//请求参数
        params.addHeader("token", MainActivity.token);//添加header信息
        JSONObject ob = new JSONObject();
        ob.put("userId", MainActivity.userid);
        ob.put("name", name);
        ob.put("phone", phone);
        ob.put("address", tv_area.getText().toString()+address);
        params.applicationJson(ob);

        HttpRequest.post(url, params, new BaseHttpRequestCallback<String>() {

            //请求网络前
            @Override
            public void onStart() {
                showdialog();
            }

            @Override
            protected void onSuccess(String str) {
                Log.e("==", str);
                JSONObject json = JSONObject.parseObject(str);
                if (json.getBoolean("successful")) {
                    ToastUtils.getInstanc(mContent).showToast("添加成功");
                    finish();
                } else {
                    String datastr = json.getString("data");
                    JSONObject data = JSONObject.parseObject(datastr);
                    ToastUtils.getInstanc(mContent).showToast(data.getString("msg"));
                }
            }

            //请求失败（服务返回非法JSON、服务器异常、网络异常）
            @Override
            public void onFailure(int errorCode, String msg) {
//                toast("网络异常~，请检查你的网络是否连接后再试");
                Log.e("==", msg);
            }

            //请求网络结束
            @Override
            public void onFinish() {
                dismissdialog();
            }
        });
    }

    private void inticity() {
        CityConfig cityConfig = new CityConfig.Builder()
                .title("选择城市")//标题
                .titleTextSize(15)//标题文字大小
//                .titleTextColor("#585858")//标题文字颜  色
                .titleBackgroundColor("#ffffff")//标题栏背景色
//                .confirTextColor("#585858")//确认按钮文字颜色
                .confirmText("确定")//确认按钮文字
                .confirmTextSize(15)//确认按钮文字大小
//                .cancelTextColor("#585858")//取消按钮文字颜色
                .cancelText("取消")//取消按钮文字
                .cancelTextSize(15)//取消按钮文字大小
                .setCityWheelType(CityConfig.WheelType.PRO_CITY_DIS)//显示类，只显示省份一级，显示省市两级还是显示省市区三级
                .showBackground(true)//是否显示半透明背景
                .visibleItemsCount(4)//显示item的数量
                .province("福建省")//默认显示的省份
                .city("厦门市")//默认显示省份下面的城市
                .district("集美区")//默认显示省市下面的区县数据
                .provinceCyclic(false)//省份滚轮是否可以循环滚动
                .cityCyclic(false)//城市滚轮是否可以循环滚动
                .districtCyclic(false)//区县滚轮是否循环滚动
//                .setCustomItemLayout(R.layout.item_city)//自定义item的布局
//                        .setCustomItemTextViewId(R.id.item_city_name_tv)//自定义item布局里面的textViewid
                .drawShadows(false)//滚轮不显示模糊效果
//                .setLineColor("#03a9f4")//中间横线的颜色
                .setLineHeigh(5)//中间横线的高度
                .setShowGAT(true)//是否显示港澳台数据，默认不显示
                .build();
        mPicker.setConfig(cityConfig);

        //监听选择点击事件及返回结果
        mPicker.setOnCityItemClickListener(new OnCityItemClickListener() {
            @Override
            public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {

                //省份
                if (province != null) {

                }

                //城市
                if (city != null) {

                }

                //地区
                if (district != null) {

                }
                tv_area.setText("" + province.toString() + city.toString() + district.toString());

            }
        });
        //显示
        mPicker.showCityPicker();
    }
}
